﻿using Md.Api.Entity.ModelDB;

namespace Md.Petapoco.Repositories.MdTrainingEducation;
/// <summary>
/// 仓储基类
/// </summary>
/// <typeparam name="M">表数据实体</typeparam>
public class BaseRep<M>
{
    /// <summary>
    /// 获取排序
    /// </summary>
    /// <param name="orderBy">要排序的字段</param>
    /// <param name="isDesc">是否降序</param>
    /// <returns>string</returns>
    public string GetOrderByDesc(string? orderBy, bool? isDesc = true)
    {
        string orderByParams = string.Empty;

        if (!string.IsNullOrWhiteSpace(orderBy))
        {
            orderByParams += " create_time ";
        }
        else
        {
            orderByParams += orderBy;
        }

        if (!isDesc.HasValue)
        {
            orderByParams += " desc ";
        }

        if (Convert.ToBoolean(isDesc))
        {
            orderByParams += "desc";
        }

        if (!Convert.ToBoolean(isDesc))
        {
            orderByParams += "asc";
        }

        string strOrderBy = string.Format(@" order by {0}", orderByParams);

        return strOrderBy;
    }
    /// <summary>
    /// 添加数据
    /// </summary>
    /// <param name="model">请求实体</param>
    /// <returns>object</returns>
    public object Insert(M model)
    {
        return MdTrainingEducationDB.GetInstance().Insert(model);
    }
    /// <summary>
    /// 通过编号删除数据
    /// </summary>
    /// <param name="id">编号</param>
    /// <returns>int</returns>
    public int Delete(long? id)
    {
        return MdTrainingEducationDB.GetInstance().Delete<M>(id);
    }
    /// <summary>
    /// 更新数据
    /// </summary>
    /// <param name="model">请求实体</param>
    /// <returns>int</returns>
    public int Update(M model)
    {
        return MdTrainingEducationDB.GetInstance().Update(model);
    }
}

